package com.cpi.cornpp.distributionfutureversion.service;

import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.distributionfutureversion.converter.DistributionFutureVersionConverter;
import com.cpi.cornpp.distributionfutureversion.converter.DistributionFutureVersionDetailConverter;
import com.cpi.cornpp.distributionfutureversion.dao.DistributionFutureVersionDAO;
import com.cpi.cornpp.distributionfutureversion.exception.DistributionFutureVersionException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.unit.service.UnitService;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;

public class DistributionFutureVersionService {
	private static final String CON_UNIT = "CON_UNIT";

	/**
	 * Metodo que agrega una distribucion a una version
	 * 
	 * @author Edgar Joao
	 * @param versionId
	 * @throws PurchaseProgramException
	 */
	public void addDistribution(int versionId) throws DistributionFutureVersionException{
		DistributionFutureVersionDAO dao = null;
		try{			
			dao = new DistributionFutureVersionDAO();
			dao.addDistribution(versionId);				
		} catch (DistributionFutureVersionException distributionFutureVersionException) {
			throw distributionFutureVersionException;
		} catch (Exception e){
			DistributionFutureVersionException distributionFutureVersionException = null;
			e.printStackTrace(System.out);
			distributionFutureVersionException = new DistributionFutureVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw distributionFutureVersionException;
		}
		
	}
	/**
	 * Metodo que elimina una distribucion de una version
	 * 
	 * @author Edgar Joao
	 * @param versionId
	 * @throws PurchaseProgramException
	 */
	public void delDistribution(int versionId) throws DistributionFutureVersionException{
		DistributionFutureVersionDAO dao = null;
		try{
			dao = new DistributionFutureVersionDAO();
			dao.delDistribution(versionId);
		} catch (DistributionFutureVersionException distributionFutureVersionException) {
			throw distributionFutureVersionException;
		} catch (Exception e){
			DistributionFutureVersionException  distributionFutureVersionException = null;
			e.printStackTrace(System.out);
			distributionFutureVersionException = new DistributionFutureVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw distributionFutureVersionException;
		}
	}
	
	public List getFuturesByVersionId(Integer versionId) throws DistributionFutureVersionException{
		DistributionFutureVersionDAO distributionFutureVersionDAO = null;
		List distributionFutureVersionDTOs = null;
		DistributionFutureVersionConverter distributionFutureVersionConverter = null;
		List listOfBalansVersions = null;
		UnitService unitService = null;
		SystemParameterCollection systemParameterCollection;
		SystemParameterBean systemParameterBean;
		double contractUnit = 0;
		try{
			distributionFutureVersionDAO = new DistributionFutureVersionDAO();
			distributionFutureVersionConverter = new DistributionFutureVersionConverter();
			unitService = new UnitService();
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(CON_UNIT);
			distributionFutureVersionDTOs = distributionFutureVersionDAO.getDistributionFuturesByVersion(versionId);
			contractUnit = unitService.getUnitQuantity(systemParameterBean.getValue());
			listOfBalansVersions = distributionFutureVersionConverter.convertDTOsToViews(distributionFutureVersionDTOs, contractUnit);										
		} catch (DistributionFutureVersionException distributionFutureVersionException) {
			throw distributionFutureVersionException;
		} catch (Exception e){
			DistributionFutureVersionException distributionFutureVersionException = null;
			e.printStackTrace(System.out);
			distributionFutureVersionException = new DistributionFutureVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw distributionFutureVersionException;
		}	
		return listOfBalansVersions;
	}
	
	public List getDistributionFutureVersionDetailsByFuturePeriodId(Integer futurePeriodId, Integer versionId) throws DistributionFutureVersionException{
		List distributionFutureVersionDetailViews = null;
		DistributionFutureVersionDAO distributionFutureVersionDAO = null;		
		DistributionFutureVersionDetailConverter distributionFutureVersionDetailConverter = null;		
		
		try{
			distributionFutureVersionDAO = new DistributionFutureVersionDAO();
			distributionFutureVersionDetailConverter = new DistributionFutureVersionDetailConverter();					
			distributionFutureVersionDetailViews = distributionFutureVersionDetailConverter.convertDTOsToViews(distributionFutureVersionDAO.getDistributionFutureDetailByFuturePeriodId(futurePeriodId, versionId));
		} catch (DistributionFutureVersionException distributionFutureVersionException) {
			throw distributionFutureVersionException;
		} catch (Exception e){
			DistributionFutureVersionException distributionFutureVersionException = null;
			e.printStackTrace(System.out);
			distributionFutureVersionException = new DistributionFutureVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw distributionFutureVersionException;
		}	
		return distributionFutureVersionDetailViews;
	}
}
